package com.poker.service;

import com.poker.entity.Activity;
import com.poker.entity.ActivitySchedule;
import com.poker.repository.ActivityRepository;
import com.poker.repository.ActivityScheduleRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.Date;

/**
 * @ClassName ActivityScheduleService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/19 16:33
 * @Version 1.0
 **/
@Service
public class ActivityScheduleService {
    @Resource
    private ActivityScheduleRepository activityScheduleRepository;

    @Resource
    private ActivityRepository activityRepository;

    public void saveActivitySchedule(ActivitySchedule activitySchedule) {
        activitySchedule.setClassify(getActivityClassify(activitySchedule.getActivityId()));
        activitySchedule.setCreateDate(new Date());
        activityScheduleRepository.saveAndFlush(activitySchedule);
    }

    public Page<ActivitySchedule> findList( Integer page, Integer size,Integer classifya) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createDate");
        if(classifya!=null){
            Specification<ActivitySchedule> spec = new Specification<ActivitySchedule>() {        //查询条件构造
                public Predicate toPredicate(Root<ActivitySchedule> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Path<Integer> classify = root.get("classify");
                    Predicate p1 = cb.equal(classify, classifya);
                    Predicate p = cb.and(p1);
                    return p;
                }
            };
            return   activityScheduleRepository.findAll(spec,pageable);
        }else {
            return activityScheduleRepository.findAll(pageable);
        }

    }

    public Page<ActivitySchedule> findListAll(Integer page, Integer size, ActivitySchedule activitySchedule, Integer classifya) {

        Pageable pageable = PageRequest.of(page, size,Sort.Direction.DESC, "createDate");
        Specification<ActivitySchedule> spec = new Specification<ActivitySchedule>() {        //查询条件构造
            public Predicate toPredicate(Root<ActivitySchedule> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> activityName = root.get("activityName");
                Path<String> scheduleName = root.get("scheduleName");
                Path<Integer> classify = root.get("classify");
                Predicate p0 = cb.equal(classify, classifya);
                Predicate p1 = cb.like(activityName, "%"+activitySchedule.getActivityName()+"%");
                Predicate p2 = cb.like(scheduleName, "%"+activitySchedule.getScheduleName()+"%");
                Predicate p = cb.and(p0,p1,p2);
                return p;
            }
        };
        return   activityScheduleRepository.findAll(spec,pageable);
    }

    public ActivitySchedule getById(String id) {
        return activityScheduleRepository.getOne(id);
    }

    public void deleteactivitySchedule(ActivitySchedule activitySchedule) {
        activityScheduleRepository.delete(activitySchedule);
    }
    public Integer getActivityClassify(String activityId){
        Activity one = activityRepository.getOne(activityId);
        return one.getClassify();
    }
}
